package com.yugao.fintech.antelope.system.handler.config;

import com.yugao.fintech.antelope.base.model.enums.ConfigKeyEnum;
import com.yugao.fintech.antelope.base.model.module.config.param.BaseConfigParam;
import com.yugao.fintech.antelope.base.model.module.config.param.PublicConfigParam;
import com.yugao.fintech.antelope.system.model.entity.SysConfig;
import com.yugao.fintech.antelope.system.model.rqrs.tenant.TenantOwnerUpdateReq;
import com.yugao.fintech.antelope.system.service.SysTenantService;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import org.springframework.stereotype.Component;


@Component
public class PublicConfigHandler extends ConfigHandler {
    @Autowired
    private SysTenantService tenantService;

    @Override
    public void doneWriteOps(SysConfig newConfig, BaseConfigParam<?> param) {
        PublicConfigParam newParam = (PublicConfigParam) param;
        TenantOwnerUpdateReq updateReq = new TenantOwnerUpdateReq();
        updateReq.setLogo(newParam.getLogo());
        updateReq.setName(newParam.getName());
        tenantService.updateOwnerTenant(updateReq);

        // 判断是否为使能存储/短信/邮箱, 如果使能某个渠道且使能的厂商发生变更, 就初始化
        if (StringUtils.isNotEmpty(newParam.getEnabledEmailConfigKey())) {
            handlerWriteOps(configManager.getByConfigKey(newParam.getEnabledEmailConfigKey()));
        }
        if (StringUtils.isNotEmpty(newParam.getEnabledStorageConfigKey())) {
            handlerWriteOps(configManager.getByConfigKey(newParam.getEnabledStorageConfigKey()));
        }
        if (StringUtils.isNotEmpty(newParam.getEnabledSmsConfigKey())) {
            handlerWriteOps( configManager.getByConfigKey(newParam.getEnabledSmsConfigKey()));
        }
    }

    @Override
    public ConfigKeyEnum configKey() {
        return ConfigKeyEnum.WEB_SITE;
    }
}
